from tools.dbo import get_server_data_car, put_mysql_data, get_server_data, carried_sql, get_server_data_one, \
    get_dict_data_sql


# 部门迁移
# select departId,departNo,departNm,uppDepartId from DV_Orgnization where isDel = 0
def syn_dept(sql_server_conn, mysql_conn):
    print("===============================正在迁移部门数据==============================")
    carried_sql(mysql_conn,"TRUNCATE sys_dept")
    uppDepartId = "case uppDepartId when 0 then 0 else uppDepartId+99 end"
    return_data = get_server_data(sql_server_conn, "2,departId+99,departNo,departNm,"+uppDepartId, "isDel = 0" , "DV_Orgnization")
    print(put_mysql_data(mysql_conn, "tenant_id,dept_id,dept_code,dept_name,parent_id", "sys_dept", return_data))
    return_data = get_dict_data_sql(mysql_conn, "select dept_id,parent_id from sys_dept")
    dept_dict = {};
    for data in return_data:
        dept_dict[data["dept_id"]] = data["parent_id"]
    aa = []
    for data in return_data:
        deot_dict_value = {};
        deot_dict_value["id"] = data["dept_id"];
        ancestors = str(deot_dict_value["id"])+","
        ancestors = syn_dept_dict(dept_dict,deot_dict_value["id"],ancestors)
        ancestors = ancestors.split(",")
        bb = ""
        for i in range(len(ancestors)-1,-1,-1):
            bb = bb + ancestors[i]+ ","
        deot_dict_value["ancestors"] = bb[1:-1]
        aa.append(deot_dict_value)
    for data in aa:
        carried_sql(mysql_conn,"update sys_dept set ancestors = '"+str(data["ancestors"])+"' where dept_id = "+str(data["id"]))
    print("===============================部门数据迁移完成==============================")

    # carried_sql(mysql_conn,"UPDATE sys_dept set dept_id = dept_id + 99,parent_id = parent_id + 99 WHERE dept_id <> 0")
    # UPDATE sys_dept set dept_id = dept_id + 99,parent_id = parent_id + 99 WHERE dept_id <> 0

# 查找部门递归函数
def syn_dept_dict(dept_dict,id,ancestors):
    if dept_dict[id] == 0:
        return str(ancestors) + str("0")+","
    s = str(dept_dict[id])+","
    return str(ancestors) + syn_dept_dict(dept_dict,dept_dict[id],s)

# 人员信息同步
def syn_user(sql_server_conn, mysql_conn):
    import os
    from hashlib import md5
    print("===============================正在迁移用户数据==============================")
    carried_sql(mysql_conn,"TRUNCATE sys_user")
    carried_sql(mysql_conn,"TRUNCATE sys_user_role")
    carried_sql(mysql_conn,"insert into sys_user (user_id,dept_id,login_name,user_name,user_type,sex,status,password,salt,tenant_id,del_flag) values (1,100,'admin','admin',0,0,0,'271db5b6dd2a977538ac33f28a66c046','3e0f39',1,0)")
    gender = "case gender when '男' then 0 else 1 end"
    isEffect = "case isEffect when 1 then 0 else 0 end"
    return_data = get_server_data(sql_server_conn, "2,empId,empNo,empNm,"+gender+",departId+99,telphone,mobile,email,style,mark,"+isEffect,"isDel = 0","DV_Eemployee")
    res = []
    for item in return_data[1:]:
        data = list(item)
        salt = os.urandom(3).hex()
        password = md5((item[2]+"123456"+salt).encode("utf8")).hexdigest()
        data.append(salt)
        data.append(password)
        res.append(data)
    carried_sql(mysql_conn,"insert into sys_user_role(role_id,user_id) values (2,"+str(return_data[1][1])+")")
    print(put_mysql_data(mysql_conn, "tenant_id,user_id,login_name,user_name,sex,dept_id,telephone_number,phonenumber,email,user_type,remark,status,salt,password", "sys_user", res))
    for i in range(1,5):
        carried_sql(mysql_conn,"insert into sys_user_role(role_id,user_id) values ("+str(i)+",1)")

    print("===============================用户数据迁移完成==============================")

# 租户数据同步
def syn_tenant(sql_server_conn, mysql_conn):
    carried_sql(mysql_conn, "TRUNCATE sys_tenant")
    carried_sql(mysql_conn,"INSERT INTO sys_tenant(`tenant_id`, `tenant_name`, `address`, `status`, `version_id`, `contact_person`, `contact_phone`, `admin_id`, `remark`, `is_delete`, `create_id`, `create_time`, `update_id`, `update_time`) VALUES (1, '海尔生物', '233', '0', '1', NULL, NULL, 1, NULL, 0, 1, '2021-04-15 16:33:40', 1, '2021-09-02 10:46:40');")
    carried_sql(mysql_conn,"INSERT INTO sys_tenant(`tenant_id`, `tenant_name`, `address`, `status`, `version_id`, `contact_person`, `contact_phone`, `admin_id`, `remark`, `is_delete`, `create_id`, `create_time`, `update_id`, `update_time`) VALUES (2, '默认租户', '233', '0', '1', NULL, NULL, 1, NULL, 0, 1, '2021-04-15 16:33:40', 1, '2021-09-02 10:46:40');")
